package crawler.searchFilter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import crawler.VideoItem;

/**
 * filter results of Baidu.
 * @author ladjzero
 *
 */
public class Baidu extends Filter{
	
	public Baidu(String keywords){
		super(keywords);
	}
	
	public ArrayList<VideoItem> getVideoList(){
		ArrayList<VideoItem> videoList = new ArrayList<VideoItem>();
		
		WebClient webClient = new WebClient();
		webClient.setJavaScriptEnabled(true);	//AJAX
		HtmlPage htmlPage;
		
		String baseUrl;
		
		try {
			baseUrl = "http://video.baidu.com/v?word=" + URLEncoder.encode( keywords, "gb2312");
			
			htmlPage = webClient.getPage(baseUrl);	
			while( true){
				Thread.sleep(5*1000);	//wait for JavaScript completely being loaded	
				
				HtmlElement resultArea = htmlPage.getElementById("results-list");
				List<HtmlElement> listOfThisPage = resultArea.getElementsByTagName("li");
				int numOfThisPage = listOfThisPage.size();
				
				for(int i=0; i<numOfThisPage; i++){
					VideoItem videoItem = new VideoItem();
					videoItem.url = ((DomElement) listOfThisPage.get(i).getFirstChild()).getAttribute("href");
					videoItem.title = ((DomElement) listOfThisPage.get(i).getFirstChild()).getAttribute("title");
					
					videoList.add(videoItem);
				}
				//load next page
				if( "#".equals( htmlPage.getElementById("nextBtn").getAttribute("href")) )
					return videoList;
				//htmlPage = nextButton.click();//nextPage button
				webClient.closeAllWindows();
				htmlPage = webClient.getPage(baseUrl+"#pn="+videoList.size());
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (com.gargoylesoftware.htmlunit.ElementNotFoundException e) {
			return videoList;
		}
		
		return videoList;
	}
}
